package net.lzzy.practicesapi1.utils;

import com.alibaba.fastjson.JSONObject;
import net.lzzy.practicesapi1.web.models.BaseInfo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

public class AspectUtil {
    public static final String TOKEN = "token";
    /**
     * 1.token直接在参数列表中
     * 2.token在BaseInfo参数类对象中
     * 3.token在Json对象中
     * @param point 业务方法参数
     * @return 解析得到的token
     */
    public static <JSONObject> String getToken(ProceedingJoinPoint point){
        Object[] args = point.getArgs();
        String[] names = ((MethodSignature)point.getSignature()).getParameterNames();
        int i = 0;
        for(String name : names){
            if(TOKEN.equals(name)){
                return args[i].toString();
            }
            i++;
        }
        if(args[0] instanceof BaseInfo){
            return ((BaseInfo)args[0]).getToken();
        }else {
            JSONObject obj = (JSONObject) args[0];
            return obj.toString();
        }


    }
}
